package com.zwf.domain.strategy.model.entity;

import com.zwf.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zwf
 * @description
 * @date 2024/7/3 14:49
 */

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntry {

    /** 抽奖策略id */
    private Long strategyId;
    /** 抽奖奖品id */
    private Integer awardId;
    /** 抽奖规则类型【1-策略规则，2-奖品规则】 */
    private Integer ruleType;
    /** 抽奖规则【rule_lock】 */
    private String ruleModel;
    /** 抽奖规则比值 */
    private String ruleValue;
    /** 抽奖规则描述 */
    private String ruleDesc;

    //解析权重规则
    public Map<String, List<Integer>> getRuleWeightValues() {
        if(!Constants.RuleModel.RULE_WEIGHT.equals(ruleModel)) return null;
        //根据空格分隔
        String[] valuesGroups = ruleValue.split(Constants.SPACE);
        if(valuesGroups ==null || valuesGroups.length==0) return null;
        //进行分组
        Map<String, List<Integer>> ruleWeightValus = new HashMap<>();
        for (String valuesGroup : valuesGroups){
            //根据冒号分隔出key和value
            String[] keyAndValue = valuesGroup.split(Constants.COLON);
            if(keyAndValue == null || keyAndValue.length!=2){
                throw new IllegalArgumentException("rule_weight invalid input format" + valuesGroups);
            }
            //对value根据逗号进行分隔
            String[] values = keyAndValue[1].split(Constants.COMMA);
            List<Integer> valuez = new ArrayList<>(values.length);
            for (String value : values){
                valuez.add(Integer.valueOf(value));
            }
            ruleWeightValus.put(keyAndValue[0],valuez);
        }
        return ruleWeightValus;
    }
}
